

class MarketRegistryController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ marketRegistryInstanceList: MarketRegistry.list( params ), marketRegistryInstanceTotal: MarketRegistry.count() ]
    }

    def show = {
        def marketRegistryInstance = MarketRegistry.get( params.id )

        if(!marketRegistryInstance) {
            flash.message = "MarketRegistry not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ marketRegistryInstance : marketRegistryInstance ] }
    }

    def delete = {
        def marketRegistryInstance = MarketRegistry.get( params.id )
        if(marketRegistryInstance) {
            try {
                marketRegistryInstance.delete(flush:true)
                flash.message = "MarketRegistry ${params.id} deleted"
                redirect(action:list)
            }
            catch(org.springframework.dao.DataIntegrityViolationException e) {
                flash.message = "MarketRegistry ${params.id} could not be deleted"
                redirect(action:show,id:params.id)
            }
        }
        else {
            flash.message = "MarketRegistry not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def marketRegistryInstance = MarketRegistry.get( params.id )

        if(!marketRegistryInstance) {
            flash.message = "MarketRegistry not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ marketRegistryInstance : marketRegistryInstance ]
        }
    }

    def update = {
        def marketRegistryInstance = MarketRegistry.get( params.id )
        if(marketRegistryInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(marketRegistryInstance.version > version) {
                    
                    marketRegistryInstance.errors.rejectValue("version", "marketRegistry.optimistic.locking.failure", "Another user has updated this MarketRegistry while you were editing.")
                    render(view:'edit',model:[marketRegistryInstance:marketRegistryInstance])
                    return
                }
            }
            marketRegistryInstance.properties = params
            if(!marketRegistryInstance.hasErrors() && marketRegistryInstance.save()) {
                flash.message = "MarketRegistry ${params.id} updated"
                redirect(action:show,id:marketRegistryInstance.id)
            }
            else {
                render(view:'edit',model:[marketRegistryInstance:marketRegistryInstance])
            }
        }
        else {
            flash.message = "MarketRegistry not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def create = {
        def marketRegistryInstance = new MarketRegistry()
        marketRegistryInstance.properties = params
        return ['marketRegistryInstance':marketRegistryInstance]
    }

    def save = {
        def marketRegistryInstance = new MarketRegistry(params)
        if(!marketRegistryInstance.hasErrors() && marketRegistryInstance.save()) {
            flash.message = "MarketRegistry ${marketRegistryInstance.id} created"
            redirect(action:show,id:marketRegistryInstance.id)
        }
        else {
            render(view:'create',model:[marketRegistryInstance:marketRegistryInstance])
        }
    }

    def registryIndex = {
    }

    def registry = {
        def marketRegistry = new MarketRegistry(fullAddress:params.marketRegistryAddress)
        marketRegistry.save()
        new LocalConfigurationRegistry(key:"address", value:params.address).save()
        def url = marketRegistry.fullAddress+"/market/registry?address="+params.address
        def post = new Post("url":url)
        def xmlResponse = post.getText()
        redirect(controller:"marketRegistry", action:"list")
    }
}
